﻿@using System.Text
@using OA.Model
@using OA.AspNetCore.Html
@using OA.Core.Model
@using OA.Core.Helper

@model OA.Model.PermissionGroup
@{

    Layout = null;
    string msg = ViewBag.Msg;
    string tips = string.Empty;
    if (!string.IsNullOrWhiteSpace(msg))
    {
        tips = $"layer.msg('{msg}');";
    }
    List<MenuInfo> selectList = ViewBag.MyMenus;
    List<string> topMenus = selectList.Where(m => !string.IsNullOrWhiteSpace(m.p) && m.c.Length == 6).Select(m => m.p).Distinct().ToList();
    List<string> codes = new List<string>();
    if (!string.IsNullOrWhiteSpace(Model.PermissionContext))
    {
        codes=JsonHelper.DeserializeList<string>(Model.PermissionContext);
    }
}
<div class="layui-form layui-tab-content" id="LAY_ucm" style="padding:0px">
    @using (Html.BeginForm(FormMethod.Post, htmlAttributes: new { @class = "layui-form", id = "myForm" }))
    {
        @Html.AntiForgeryToken()
        <div class="layui-form-item">
            @Html.LabelFor(model => model.Name, new { @class = "layui-form-label" })
            <div class="layui-input-block">
                @Html.HiddenFor(model => model.ID)
                @Html.MyEditorFor(model => model.Name)
            </div>
        </div>
        <div class="layui-form-item">
            @Html.LabelFor(model => model.PermissionContext, new { @class = "layui-form-label" })
            <div class="layui-input-block">
                <div class="layui-collapse">
                    @foreach (string topMenu in topMenus)
                    {
                        <div class="layui-colla-item">

                            <h2 class="layui-colla-title">@topMenu</h2>
                            <div class="layui-colla-content">
                                <table class="layui-table">
                                    <colgroup>
                                        <col width="60">
                                        <col width="150">
                                        <col>
                                        <col>
                                    </colgroup>
                                    <thead>
                                        <tr>
                                            <th>选择</th>
                                            <th>编码</th>
                                            <th>名称</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        @{
                                            List<MenuInfo> children = selectList.Where(m => m.p == topMenu).ToList();
                                            foreach (MenuInfo child in children)
                                            {
                                                var leafNodes = selectList.Where(m => m.c.StartsWith(child.c)).OrderBy(m => m.c).ToList();
                                                if (leafNodes.Count == 1)
                                                {
                                                    MenuInfo leafNode = leafNodes[0];
                                                    string isChecked = "";
                                                    if (codes.Contains(leafNode.c))
                                                    {
                                                        isChecked = "checked";
                                                    }

                                                    <tr>
                                                        <td><input type="checkbox" name="codes" value="@leafNode.c" lay-skin="primary" @isChecked class="mycode" /></td>
                                                        <td>@leafNode.c</td>
                                                        <td>@leafNode.n</td>
                                                    </tr>

                                                }
                                                else
                                                {
                                                    <tr>
                                                        <td colspan="3">
                                                            <div class="layui-colla-item">
                                                                <h2 class="layui-colla-title">@child.n</h2>
                                                                <div class="layui-colla-content">

                                                                    <table class="layui-table">
                                                                        <colgroup>
                                                                            <col width="60">
                                                                            <col width="150">
                                                                            <col>
                                                                            <col>
                                                                        </colgroup>
                                                                        <thead>
                                                                            <tr>
                                                                                <th>选择</th>
                                                                                <th>编码</th>
                                                                                <th>名称</th>
                                                                            </tr>
                                                                        </thead>
                                                                        <tbody>
                                                                            @{

                                                                                foreach (MenuInfo leafNode in leafNodes)
                                                                                {
                                                                                    string isChecked = "";
                                                                                    if (codes.Contains(leafNode.c))
                                                                                    {
                                                                                        isChecked = "checked";
                                                                                    }
                                                                                    <tr>
                                                                                        <td><input type="checkbox" name="codes" value="@leafNode.c" lay-skin="primary" @isChecked class="mycode" /></td>
                                                                                        <td>@leafNode.c</td>
                                                                                        <td>@leafNode.n</td>
                                                                                    </tr>
                                                                                }
                                                                            }
                                                                        </tbody>
                                                                    </table>

                                                                </div>
                                                            </div>
                                                        </td>
                                                    </tr>
                                                }

                                            }
                                        }
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    }
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <input type="submit" value="提 交" lay-submit="" lay-filter="OAForm" class="layui-btn mySubmit" />
            </div>
        </div>
    }
</div>
